package com.aizhixin.lab.course.template.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.PageData;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.course.dto.CourseAssistDirDTO;
import com.aizhixin.lab.course.template.dto.TemplateAssistDirDTO;
import com.aizhixin.lab.course.template.dto.TemplateAssistFileDTO;
import com.aizhixin.lab.course.template.entity.TemplateAssist;
import com.aizhixin.lab.course.template.service.TemplateAssistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/v1/course/template/assist")
@Api(value = "课程模板辅助资料相关API")
public class TemplateAssistController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private TemplateAssistService assistService;

    @RequestMapping(value = "/getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程列表", response = Void.class, notes = "查询课程列表<br>@author hsh")
    public ResponseEntity<?> getList(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "templateId", required = false) @RequestParam(value = "templateId", required = false) Long templateId,
                                     @ApiParam(value = "parentId", required = false) @RequestParam(value = "parentId", required = false) Long parentId,
                                     @ApiParam(value = "pageNumber 起始页") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
                                     @ApiParam(value = "pageSize 每页的限制数目") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        PageData<TemplateAssist> result = assistService.getList(templateId, parentId, pageNumber, pageSize);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveDir", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存文件夹", response = Void.class, notes = "保存文件夹<br>@author hsh")
    public ResponseEntity<?> saveDir(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "dir", required = false) @RequestBody TemplateAssistDirDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = assistService.saveDir(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/updateDir", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "修改文件夹", response = Void.class, notes = "修改文件夹<br>@author hsh")
    public ResponseEntity<?> updateDir(@RequestHeader("Authorization") String token,
                                       @ApiParam(value = "dir", required = false) @RequestBody CourseAssistDirDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = assistService.updateDir(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存文件", response = Void.class, notes = "保存文件<br>@author hsh")
    public ResponseEntity<?> saveDir(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "file", required = false) @RequestBody TemplateAssistFileDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = assistService.saveFile(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/saveFileList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "批量保存文件", response = Void.class, notes = "保存文件<br>@author hsh")
    public ResponseEntity<?> saveDirList(
            @ApiParam(value = "file", required = false)  TemplateAssistFileDTO dto) {
       //todo 批量上传文件

        Map<String, Object> result = assistService.saveFile(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除", response = Void.class, notes = "删除<br>@author hsh")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "assistId", required = false) @RequestParam(value = "assistId", required = false) Long assistId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = assistService.delete(assistId);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}
